package com.topfinance.cnaps2.web.config;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.topfinance.frame.util.StringUtil;
import com.topfinance.frame.web.BaseSupport;
import com.topfinance.payment.ebo.TCfgNodeEbo;
import com.topfinance.payment.service.ITCfgNodeMgr;
import com.opensymphony.xwork2.ModelDriven;

/**
 * <p>
 * Description:
 * </p>
 * 
 * @author
 * @version 1.50
 *          <p>
 *          Copyright: Copyright (c) 2004 2005
 *          </p>
 *          <p>
 *          Company:
 *          </p>
 */
public final class CfgNodeAction extends BaseSupport implements ModelDriven<TCfgNodeEbo> {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4650749921776542075L;

	private ITCfgNodeMgr nodeManager = null;

	private TCfgNodeEbo nodeEbo = new TCfgNodeEbo();

	public TCfgNodeEbo getModel() {
		return nodeEbo;
	}

	public void setNodeManager(ITCfgNodeMgr nodeManager) {
		this.nodeManager = nodeManager;
	}

	public CfgNodeAction() {
		super();
	}

	public String execute() {
		return list();
	}

	public String list() {
		log.debug("CfgTransAction-list begin");
		try {
			List l = nodeManager.getAllNode();
			rtnResultMap = new HashMap();
			rtnResultMap.put(DATA_LIST_ROOT, l);
			rtnResultMap.put(RECORDS_COUNT, l.size());
		} catch (Exception e) {
			handleException(e);
		}
		log.debug("CfgTransAction-list end");
		return SUCCESS;
	}
	
	public String liststruct() {
		log.debug("CfgTransAction-struct begin");
		try {
			List l = StringUtil.mapClass(TCfgNodeEbo.class);
			log.debug("[ConfNodeEbo Struct]" + l.size());
			rtnResultMap = new HashMap();
			rtnResultMap.put(DATA_LIST_ROOT, l);
			rtnResultMap.put(RECORDS_COUNT, l.size());
		} catch (Exception e) {
			handleException(e);
		}
		log.debug("CfgTransAction-struct end");
		return SUCCESS;
	}

	public String create() {
		try {
			log.debug(StringUtil.mapObject(nodeEbo));
			nodeManager.addNode(nodeEbo);
			rtnResultMap = new HashMap();
			rtnResultMap.put("success", "true");
			String[] args = new String[1];
			args[0] = "" + nodeEbo.getOid();
			rtnResultMap.put("info", getText(CREATE_SUCCESS_KEY, args));
		} catch (Exception e) {
			handleException(e);
		}
		return SUCCESS;
	}

	public String update() {
		try {
			log.debug(StringUtil.mapObject(nodeEbo));
			nodeManager.editNode(nodeEbo);
			rtnResultMap = new HashMap();
			rtnResultMap.put("success", "true");
			String[] args = new String[1];
			args[0] = "";// you can add extra message here.
			rtnResultMap.put("info", getText(UPDATE_SUCCESS_KEY, args));
		} catch (Exception e) {
			handleException(e);
		}
		return SUCCESS;
	}

	public TCfgNodeEbo getNodeEbo() {
		return nodeEbo;
	}

	public void setNodeEbo(TCfgNodeEbo nodeEbo) {
		this.nodeEbo = nodeEbo;
	}

	public String query() {
		if (request.getParameter("id") == null) {
			rtnResultMap = new HashMap();
			String[] args = new String[1];
			args[0] = "";// you can add extra message here
			rtnResultMap.put("info", getText("expect.valid.pk.data", args));
			return SUCCESS;
		}
		try {
			String uid = request.getParameter("id");
			TCfgNodeEbo trans = nodeManager.getNode(new Integer(uid));
			List l = new ArrayList<TCfgNodeEbo>();
			l.add(trans);
			rtnResultMap = new HashMap();
			rtnResultMap.put(DATA_LIST_ROOT, l);
			rtnResultMap.put(RECORDS_COUNT, 1);
		} catch (Exception e) {
			handleException(e);
		}
		return SUCCESS;
	}
	
	public String delete() {
		try {
			log.debug(StringUtil.mapObject(nodeEbo));
			String delId = request.getParameter("id");
			System.out.println("============================"+delId);
			nodeManager.deleteNode(new Integer(delId));
			rtnResultMap = new HashMap();
			rtnResultMap.put("success", "true");
			String[] args = new String[1];
			args[0] = "";// you can add extra message here.
			rtnResultMap.put("info", getText(DELETE_SUCCESS_KEY, args));
		} catch (Exception e) {
			handleException(e);
		}
		return SUCCESS;
	}	
	
	public String test() {
		log.debug("SysFuncAction-test begin");
		try {
			TCfgNodeEbo nEbo = new TCfgNodeEbo();
			nEbo.setName("对公网银");
			nEbo.setType("1");
			nEbo.setIp("10.11.12.11");
			nodeManager.save(nEbo);
			rtnResultMap = new HashMap();
			rtnResultMap.put("info", getText("create.success", "Test Success!"));
			return SUCCESS;
		} catch (Exception e) {
			handleException(e);
		}
		log.debug("SysFuncAction-list end");
		return SUCCESS;
	}
	
}
